package com.example.demo.controller;

import com.example.demo.dto.CourseInfo;
import com.example.demo.dto.CourseVo;
import com.example.demo.entity.Course;
import com.example.demo.service.CourseService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
@CrossOrigin
public class CourseController {

    @Autowired
    private CourseService courseService;

    // 新增课程
    @PostMapping("add")
    public int insertCourse(@RequestBody CourseVo courseVo) {
        if(courseVo.getStatus().equals("已发布")) courseVo.setStatus("published");
        else courseVo.setStatus("unpublished");
        return courseService.insertCourse(courseVo);
    }

    // 删除课程
    @DeleteMapping("/{id}")
    public int deleteCourseById(@PathVariable int id) {
        return courseService.deleteCourseById(id);
    }

    // 修改课程
    @PutMapping
    public int updateCourse(@RequestBody Course course) {
        return courseService.updateCourse(course);
    }

    // 查询课程
    @GetMapping("/{id}")
    public Course selectCourseById(@PathVariable int id) {
        return courseService.selectCourseById(id);
    }

    // 查询所有课程
//    @GetMapping("findAll")
//    public List<Course> selectAllCourses() {
//        return courseService.selectAllCourses();
//    }

    // 联合查询课程及其教师和分类信息
    @GetMapping("findAll")
    public List<CourseInfo> selectCourseWithTeacherAndCategory() {
        List<CourseInfo> list = courseService.getAllCourses();
        for(CourseInfo x : list) {
            System.out.println(x);
        }
        return courseService.getAllCourses();
    }
}
